package com.whf.Controller;

import com.whf.config.WeChatMpConfig;
import com.whf.enums.ResultEnum;
import com.whf.exception.SellException;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;

/**
 * 通过第三方sdk完成网页授权
 */
@Controller
@RequestMapping("/wechat")
@Slf4j
public class WeChatController {
    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxMpService wxOpenService;

    //发起页面授权验证
    @GetMapping("/authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        //1.配置
        //此处使用配置文件+实体类+读取配置方法类实现,实现后直接将对象返回
        //2.调用方法
        String url="http://whfapp.natapp1.cc/sell/wechat/userInfo";
        //第一个参数是回调网址,第二个参数指定是否需要通过用户授权获取用户信息,第三个参数是自定义回调网址时代的参数内容
        String redirectUrl=wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, URLEncoder.encode(returnUrl));
        log.info("[微信网页授权]获取redirectUrl:{}",redirectUrl);
        //先跳转到redirectUrl.然后会自动跳转到回调网址,带上code和state
        return "redirect:"+redirectUrl;
    }
    //重定向
    @GetMapping("/userInfo")
    public String userInfo(@RequestParam("code") String code,@RequestParam("state") String returnUrl){
        log.info("[获取用户信息] code={}",code);
        log.info("[获取用户信息] returnUrl={}",returnUrl);
        //获取openid
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken =new WxMpOAuth2AccessToken();
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        } catch (WxErrorException e) {
            log.error("[网页授权] e:{}",e);
            throw new SellException(ResultEnum.WECHAT_ERROR_MP.getCode(),e.getMessage());
        }

        String openId=wxMpOAuth2AccessToken.getOpenId();
        String url="redirect:"+returnUrl+"?openid="+openId;
        //重定向
        return url;
    }


    @GetMapping("/qrAuthorize")
    public String qrAuthorize(@RequestParam("returnUrl") String returnUrl){
        String url="http://whfapp.natapp1.cc/sell/wechat/userInfo";
        String redirectUrl=wxOpenService.oauth2buildAuthorizationUrl(url, WxConsts.QrConnectScope.SNSAPI_LOGIN, URLEncoder.encode(returnUrl));
        log.info("[微信网页授权]获取redirectUrl:{}",redirectUrl);
        return "redirect:"+redirectUrl;
    }
    //重定向
    @GetMapping("/qrUserInfo")
    public String qrUserInfo(@RequestParam("code") String code,@RequestParam("state") String returnUrl){
        //获取openid
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken =new WxMpOAuth2AccessToken();
        try {
            wxMpOAuth2AccessToken = wxOpenService.oauth2getAccessToken(code);
        } catch (WxErrorException e) {
            log.error("[网页授权] e:{}",e);
            throw new SellException(ResultEnum.WECHAT_ERROR_MP.getCode(),e.getMessage());
        }

        String openId=wxMpOAuth2AccessToken.getOpenId();
        String url="redirect:"+returnUrl+"?openid="+openId;
        //重定向
        return url;
    }



}
